package com.spring.sperapc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.User;
import com.spring.security.web.security.Contents;
import com.spring.sperapc.web.model.UserDetail;
import com.spring.sperapc.web.service.UserDetailService;

/**
 * Created by wojunyu on 2015/8/12.
 */
@Controller
@RequestMapping(value = "/userDetail")
@ApiIgnore
public class UserDetailController {
    static Logger logger = Logger.getLogger(UserDetailController.class.getName());

    @Resource
    private UserDetailService userDetailService;

    /**
     * 跳转到维护界面
     * @return
     */
    @RequestMapping(value = "/updateForm")
    public ModelAndView updateForm(HttpServletRequest request){
        Map<String, Object> retD = new HashMap<String, Object>();
        try{
            User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
            if(!ApplicationUtils.isNotNull(u.getId())){
                retD.put("msg","用户未登录,请先登录");
                return new ModelAndView("login",retD);
            }

            UserDetail userDetail = userDetailService.selectByPrimaryKey(u.getId());
            retD.put("dataSet",userDetail);
        }catch (Exception e){
            e.printStackTrace();
        }

        return new ModelAndView("user/detail_update",retD);
    }

    /**
     * 新增用户信息
     * @param userDetail
     * @return
     */
    @RequestMapping(value = "/saveUserDetail")
    public ModelAndView saveUserDetail(UserDetail userDetail,HttpServletRequest request){
        User u = (User)request.getSession().getAttribute(Contents.USER_INFO);
        userDetail.setcUserId(u.getId());
        String ret = "";
        Map<String, Object> retD = new HashMap<String, Object>();
        int iss = 0;
        if(ApplicationUtils.isNotNull(userDetail.getId())){
            iss = userDetailService.updateByPrimaryKeySelective(userDetail);
        }else{
            userDetail.setId(ApplicationUtils.randomUUID());
            iss = userDetailService.insertSelective(userDetail);
        }

        if(iss==0){
            ret = "保存失败";
        }else{
            ret = "保存成功";
        }
        retD.put("status", iss+"");
        retD.put("msg", ret);
        UserDetail user = userDetailService.selectByPrimaryKey(u.getId());
        retD.put("dataSet",user);
        return new ModelAndView("user/detail_show",retD);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteUserDetail")
    public ModelAndView deleteUserDetail(String id){
        String ret = "";
        Map<String, Object> retD = new HashMap<String, Object>();
        int iss = userDetailService.deleteByPrimaryKey(id);
        if(iss==0){
            ret = "删除失败";
        }else{
            ret = "删除成功";
        }
        retD.put("status", iss+"");
        retD.put("msg", ret);
        return new ModelAndView("user/detail_show",retD);
    }
}
